package com.jg.xmlSchema2JsonSchema.xsd;

import cn.hutool.core.util.StrUtil;
import com.jg.xmlSchema2JsonSchema.xsd.util.XsdUtil;
import org.w3c.dom.Element;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;

public class XsAttribute {
	private Element element;
	// 显示名称
	private String displayName;
	private String name;
	private String type;
	private String use;
	private String defaultValue;
	private String annotation;
	private String fixed;
	private String form;
	private String id;
	private String ref;

	public XsAttribute(Element element) {
		this.element = element;
		getData();
	}

	private void getData() {
		XsAnnotation xsAnnotation = XsdUtil.getAnnotation(element);
		if (xsAnnotation != null) {
			annotation = xsAnnotation.getText();
		}
		name = element.getAttribute("name");
		type = element.getAttribute("type");
		if (StrUtil.isEmpty(type)) {
			XsSimpleType xsSimpleType = XsdUtil.getSimpleType(element);
			if (xsSimpleType == null) {
				type = "xs:string";
			} else {
				if (xsSimpleType.getRestriction() != null) {
					type = xsSimpleType.getRestriction().getBase();
				}
			}
		}
		use = element.getAttribute("use");
		defaultValue = element.getAttribute("default");
		fixed = element.getAttribute("fixed");
		form = element.getAttribute("form");
		id = element.getAttribute("id");
		ref = element.getAttribute("ref");
	}

	public String getName() {
		return name;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String newDisplayName) {
		displayName = newDisplayName;
	}

	public String getType() {
		return type;
	}

	public String getUse() {
		return use;
	}

	public String getDefault() {
		return defaultValue;
	}

	public String getAnnotation() {
		return annotation;
	}

	public String getFixed() {
		return fixed;
	}

	public String getForm() {
		return form;
	}

	public String getId() {
		return id;
	}

	public String getRef() {
		return ref;
	}

	public XsSimpleType getSimpleType() {
		return XsdUtil.getSimpleType(element);
	}

	public Element getXmlElement() {
		return element;
	}

	public boolean isRequired() {
		return "required".equals(use);
	}

	public boolean isOptional() {
		return "optional".equals(use);
	}

	public boolean isProhibited() {
		return "prohibited".equals(use);
	}

	/*
	 * <xs:restriction base="xs:string"> <xs:length value="1111"/> <xs:minLength
	 * value="111"/> <xs:maxLength value="11"/> <xs:whiteSpace value="replace"/>
	 * </xs:restriction>
	 */
	private XsRestriction getXsRestriction() {
		XsSimpleType xsSimpleType = getSimpleType();
		if (ObjectUtil.isNotNull(xsSimpleType)) {
			return xsSimpleType.getRestriction();
		}
		return null;
	}

	public Integer getMinLength() {
		XsRestriction xsRestriction = getXsRestriction();
		if (ObjectUtil.isNotNull(xsRestriction)) {
			XsMinLength xsMinLength = xsRestriction.getXsMinLength();
			if (ObjectUtil.isNotNull(xsMinLength)) {
				if (NumberUtil.isInteger(xsMinLength.getValue())) {
					return NumberUtil.parseInt(xsMinLength.getValue());
				}
			}
		}
		return null;
	}

	public Integer getMaxLength() {
		XsRestriction xsRestriction = getXsRestriction();
		if (ObjectUtil.isNotNull(xsRestriction)) {
			XsMaxLength xsMaxLength = xsRestriction.getXsMaxLength();
			if (ObjectUtil.isNotNull(xsMaxLength)) {
				if (NumberUtil.isInteger(xsMaxLength.getValue())) {
					return NumberUtil.parseInt(xsMaxLength.getValue());
				}
			}
		}
		return null;

	}

	public Integer getLength() {
		XsRestriction xsRestriction = getXsRestriction();
		if (ObjectUtil.isNotNull(xsRestriction)) {
			XsLength xsLength = xsRestriction.getXsLength();
			if (ObjectUtil.isNotNull(xsLength)) {
				if (NumberUtil.isInteger(xsLength.getValue())) {
					return NumberUtil.parseInt(xsLength.getValue());
				}
			}
		}
		return null;
	}

	public String getWhiteSpace() {
		XsRestriction xsRestriction = getXsRestriction();
		if (ObjectUtil.isNotNull(xsRestriction)) {
			XsWhiteSpace xsWhiteSpace = xsRestriction.getXsWhiteSpace();
			if (ObjectUtil.isNotNull(xsWhiteSpace)) {
				return xsWhiteSpace.getValue();
			}
		}
		return null;
	}
}
